package singleton

import (
	"fmt"
	"io"
	"os"
	"sync"
)

var (
	generatesqlFile  *os.File
	generatesqlMutex *sync.RWMutex
)

func WriteToGeneratesqlFile(s string) {
	generatesqlMutex.Lock()
	_, err1 := io.WriteString(generatesqlFile, fmt.Sprintf("%s\n", s)) //写入文件(字符串)
	generatesqlMutex.Unlock()
	check(err1)
}

func InitGeneratesql() {
	filename := "e:/tmp2/generatesql.txt"
	var err1 error

	generatesqlMutex = new(sync.RWMutex)
	if checkFileIsExist(filename) { //如果文件存在
		generatesqlFile, err1 = os.OpenFile(filename, os.O_APPEND, 0666) //打开文件
		fmt.Println("文件存在")
	} else {
		generatesqlFile, err1 = os.Create(filename) //创建文件
		fmt.Println("文件不存在")
	}
	check(err1)
}

func CloseGeneratesql() {
	generatesqlFile.Close()
}
